package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.OaWorkPlan;
import com.ruoyi.system.service.IOaWorkPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 工作计划管理控制器
 *
 * @author 自定义
 * @date 2025-04-21
 */
@RestController
@RequestMapping("/system/workPlan")
public class OaWorkPlanController extends BaseController {

    @Autowired
    private IOaWorkPlanService oaWorkPlanService;

    /**
     * 查询工作计划列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OaWorkPlan oaWorkPlan) {
        startPage();
        List<OaWorkPlan> list = oaWorkPlanService.selectOaWorkPlanList(oaWorkPlan);
        return getDataTable(list);
    }

    /**
     * 根据ID获取工作计划详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(oaWorkPlanService.selectOaWorkPlanById(id));
    }

    /**
     * 新增工作计划
     */
    @PostMapping
    public AjaxResult add(@RequestBody OaWorkPlan oaWorkPlan) {
        // 设置创建时间为当前时间
        oaWorkPlan.setCreateTime(new Date());

        // 获取当前登录用户的 ID，假设若依框架提供了获取用户 ID 的方法
        Long userId = this.getUserId();
        oaWorkPlan.setUserId(userId);

        int rows = oaWorkPlanService.insertOaWorkPlan(oaWorkPlan);
        if (rows > 0) {
            // 返回包含新工作计划 id 的数据
            System.out.println("工作计划添加成功，新工作计划 id: " + oaWorkPlan.getId());
            return AjaxResult.success("工作计划添加成功", oaWorkPlan.getId());
        } else {
            System.out.println("工作计划添加失败");
            return AjaxResult.error("工作计划添加失败");
        }
    }

    /**
     * 修改工作计划
     */
    @PutMapping
    public AjaxResult edit(@RequestBody OaWorkPlan oaWorkPlan) {
        return toAjax(oaWorkPlanService.updateOaWorkPlan(oaWorkPlan));
    }

    /**
     * 删除工作计划
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(oaWorkPlanService.deleteOaWorkPlanByIds(ids));
    }

    /**
     * 根据用户ID查询工作计划列表
     */
    @GetMapping("/user/{userId}")
    public TableDataInfo listByUserId(@PathVariable Long userId) {
        startPage();
        List<OaWorkPlan> list = oaWorkPlanService.selectOaWorkPlanListByUserId(userId);
        TableDataInfo dataTable = getDataTable(list);
        System.out.println("返回的工作计划列表数据: " + dataTable); // 添加日志输出
        return dataTable;
    }
}